
#ifdef ignore
#undef ignore
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

#include "common.h"
#include "logger.h"
#include "DataReportManager.h"
#include "timer.h"
#include "ReportProtocol.h"

void *TimerMain(void *h)
{
    LogInfo("create time main pthread success");
    while ( TimerPtr->intializeState() )
    {
        TimerPtr->handleTick();
        usleep(10000);
    }
    return NULL;
}

void *uploadMain(void *h)
{
    LogInfo("create upload thread success");
    while( ReportInterfacePtr-> initialzedM() )
    {
        sem_wait(ReportInterfacePtr->getSem());

        while( ReportInterfacePtr-> initialzedM() )
        {
            if ( ReportInterfacePtr->ReportPrepare() )
            {
                if ( ReportInterfacePtr->DataReport() ) 
                {
                    ReportInterfacePtr->SetReportState(true);
                    break;
                }  
                else
                {
                   sleep(1); 
                }
            }
            else
            {
                sleep(1);
            }

        }
     //  ReportInterfacePtr->SetReportState(true); //for test 
    }
    return NULL;
}

int main()
{
    pthread_t pthrd;
    pthread_t upload_thrd;
    LoggerPtr->initialize();
    LoggerPtr->SetLogLevel(7);
    MsgHandlerPtr->initialize();
    if ( !TimerPtr->initialize() )
    {
        LogInfo("timer initialized failed");
        return -1;
    }
    else
    {
        LogInfo("timer initialized successed");
    }
    pthread_create(&pthrd, NULL, TimerMain, NULL);
    if ( !ReportInterfacePtr->initialze() )
    {
        LogError("ReportInterfacePtr initialzed failed");
        return -1;
    }
    pthread_create(&upload_thrd, NULL, uploadMain, NULL);
    
    MsgHandlerPtr->HandleMsg();  

}
